﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace System.ComponentModel.Composition.Factories
{
    partial class PartDefinitionFactory
    {
        private class DerivedComposablePartDefinition : ComposablePartDefinition
        {
            private readonly Func<ComposablePart> _partCreator;
            private readonly IDictionary<string, object> _metadata;
            private readonly IEnumerable<ImportDefinition> _importDefinitions;
            private readonly IEnumerable<ExportDefinition> _exportDefinitions;

            public DerivedComposablePartDefinition(
                IDictionary<string, object> metadata,
                Func<ComposablePart> partCreator,
                IEnumerable<ImportDefinition> importDefinitions,
                IEnumerable<ExportDefinition> exportDefinitions)
            {
                this._metadata = metadata.AsReadOnly();
                this._partCreator = partCreator;
                this._importDefinitions = importDefinitions ?? Enumerable.Empty<ImportDefinition>();
                this._exportDefinitions = exportDefinitions ?? Enumerable.Empty<ExportDefinition>();
            }

            public override IDictionary<string, object> Metadata
            {
                get { return this._metadata; }
            }

            public override IEnumerable<ExportDefinition> ExportDefinitions
            {
                get { return this._exportDefinitions; }
            }

            public override IEnumerable<ImportDefinition> ImportDefinitions
            {
                get { return this._importDefinitions; }
            }

            public override ComposablePart CreatePart()
            {
                return this._partCreator();
            }
        }
    }
}